package yxy.game.pm2.active.command.belovedhero;

import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.actfavoritewarlorddata;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_心仪武将活动;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.currency.Currencies;
import yxy.game.pm2.bean.timeline.Timeline;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ActiveMessage;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.battle.FighterModule;

import java.util.ArrayList;
import java.util.Map;

/****************************************
 * @name CMD_Active_心仪武将_ActBelovedHeroBattleInfo
 * @description 心仪武将武将试炼页面信息
 * @author 梁健彬
 * @since 2022-07-7
 ***************************************/
@MessageType(proto = Command.ActBelovedHeroBattleInfo, classtype = ActiveMessage.ActBelovedHeroBattleInfoRequest.class)
public class CMD_Active_心仪武将_ActBelovedHeroBattleInfo extends Command<ActiveMessage.ActBelovedHeroBattleInfoRequest> {

    @Override
    public void execute(MessageSource messageSource, ActiveMessage.ActBelovedHeroBattleInfoRequest request) throws Exception {
        String playercode = messageSource.getTag();
        ActiveMessage.ActBelovedHeroBattleInfoResponse.Builder response = ActiveMessage.ActBelovedHeroBattleInfoResponse.newBuilder();

        try {
            ActiveModule_心仪武将活动 am = ActiveModule_心仪武将活动.get(playercode, false);

            if (am == null) {
                send(messageSource, response, Result.ActNoOpen);
                return;
            }

            if (!am.running()) {
                send(messageSource, response, Result.ActNoOpen);
                return;
            }

            ActBelovedHeroHandle heroHandle = new ActBelovedHeroHandle();

            //怪物集合 （每个元素包含奖励  页面显示的boss头像   队伍信息   战力信息）
            Map<String, actfavoritewarlorddata> allData = DataCenter.getAllData(actfavoritewarlorddata.class);

            for (actfavoritewarlorddata value : allData.values()) {
                ActiveMessage.MonsterMsg.Builder monsterList = ActiveMessage.MonsterMsg.newBuilder();

                long totalPower = 0;//战力
                //封装怪物信息
                ArrayList<FighterModule> moduleList = heroHandle.getMonsterFighterModuleList(value.getId());
                for (int i = 0; i < moduleList.size(); i++) {
                    ActiveMessage.Monster.Builder monster = ActiveMessage.Monster.newBuilder();
                    monster.setHeroid(moduleList.get(i).getHeroId());
                    monster.setStar(moduleList.get(i).getStarlv());
                    monster.setLevel(moduleList.get(i).getLevel());
                    monsterList.addMonster(monster.build());
                    totalPower += moduleList.get(i).getTotalPower();
                }
                //封装奖励信息
                Currencies rewards = new Currencies(Currency.create(value.getRewardtype(), value.getRewardid(), value.getRewardnum()));
                monsterList.addAllItem(new CurrencyHandle().packItems(rewards));
                //战力信息
                monsterList.setPower(totalPower);
                //头像信息
                monsterList.setHeadId(value.getHeadid());
                //表id
                monsterList.setId(value.getId());
                response.addMonsterMsg(monsterList);
            }

            //开始结束时间
            Timeline timeline = am.getTimeline();
            long startTime = timeline.getStartTime().getMillis();
            long endTime = timeline.getEndTime().getMillis();

            //返回
            response.setProcess(am.getProcess());
            response.setBeginTime(startTime);
            response.setEndTime(endTime);

            send(messageSource, response, Result.Success);
        } catch (Exception e) {
            e.printStackTrace();
            send(messageSource, response, Result.DataError);
        }
    }

    private void send(MessageSource messageSource, ActiveMessage.ActBelovedHeroBattleInfoResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }
}
